/*
 * @company sinobest
 * @author  luxunheng
 * @date    2005-10-26
 */
package cn.com.hnisi.framework.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class SpringUtil implements ApplicationContextAware {
    private static final String[] CONTEXT_PATHS = {
    		//"classpath:spring/*.xml",
    		"classpath:spring/applicationContext.xml",
    		"classpath:spring/applicationContext-jbpm.xml",
    		"classpath:spring/applicationContext-datasource.xml",
    		//"classpath:spring/applicationContext-ut-dev.xml",
    		"classpath:spring/applicationContext-hibernate.xml",
    		"classpath:spring/applicationContext-tx.xml",    		
            "classpath:spring/applicationContext-service.xml",
            "classpath:spring/applicationContext-aop.xml"
            //"classpath:spring/applicationContext-ut.xml"
        };
    private static ApplicationContext ctx;

    /**
     * just for Spring init web application context
     */
    public void setApplicationContext(ApplicationContext webApplicationContext)
        throws BeansException {
        ctx = webApplicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        if (null == ctx) {
            // ctx = new ClassPathXmlApplicationContext(CONTEXT_PATHS);
            ctx = new org.apache.xbean.spring.context.ClassPathXmlApplicationContext(CONTEXT_PATHS);
        }

        return ctx;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
    /**
     * 因为$符号出现在spring配置文件中，会被当作   符
     * 所以我们在spring配置文件中用:代替
     * @param src
     * @return
     */
    public static String unEscapeSpringXml(String src) {
		return src.replace(":{", "${");
	}
    /**
     * 因为$符号出现在spring配置文件中，会被当作   符
     * 所以我们在spring配置文件中用:代替
     * @param src
     * @return
     */
    public static String escapeSpringXml(String src) {
		return src.replace("${", ":{");
	}
}
